package com.bstek.uflo.form.controller;

import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import com.bstek.dorado.web.resolver.AbstractResolver;
import com.bstek.uflo.command.CommandService;
import com.bstek.uflo.form.command.GetTableDefinitionCommand;
import com.bstek.uflo.form.model.TableDefinition;

/**
 * @author Jacky.gao
 * @since 2013年11月11日
 */
public class TableFieldController extends AbstractResolver {
	private CommandService commandService;
	@Override
	protected ModelAndView doHandleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String tableId=request.getParameter("tableId");
		if(StringUtils.isEmpty(tableId)){
			throw new IllegalArgumentException("Table id can not be null.");
		}
		TableDefinition table=commandService.executeCommand(new GetTableDefinitionCommand(Long.valueOf(tableId)));
		ObjectMapper mapper=new ObjectMapper();
		response.setContentType("text/json");  
		response.setCharacterEncoding("UTF-8");  
		OutputStream out=response.getOutputStream();
		try{
			mapper.writeValue(out, table);
		}finally{
			out.flush();
			out.close();
		}
		return null;
	}
	public CommandService getCommandService() {
		return commandService;
	}
	public void setCommandService(CommandService commandService) {
		this.commandService = commandService;
	}
}
